package com.ogawa.project6602.activity

import android.annotation.SuppressLint
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.base.bean.LoadState
import com.ogawa.base.constants.Constants
import com.ogawa.project6602.R
import com.ogawa.project6602.base.Base6602MqttActivity
import com.ogawa.project6602.bean.Massage6602Armchair
import com.ogawa.projectcommon.bean.CollectBean
import com.ogawa.projectcommon.constants.Constant
import com.ogawa.projectcommon.datacollect.DataCollectUtils
import com.ogawa.projectcommon.utils.ProjectSpUtils
import kotlinx.android.synthetic.main.activity_6602_device_main.iv_collect
import kotlinx.android.synthetic.main.activity_6602_seat_adjust.*

@Route(path = UrlPathConstant.ACTIVITY_6602_SEAT_ADJUST)
class Device6602SeatAdjustActivity : Base6602MqttActivity(), View.OnClickListener {
    private lateinit var tvCurrentPro: TextView
    private lateinit var tvRemainTime: TextView
    private lateinit var tvReset: TextView
    private lateinit var tvZero1: TextView
    private lateinit var tvZero2: TextView
    private lateinit var tvDaobei: TextView
    private lateinit var tvShenbei: TextView
    private lateinit var tvTaitui: TextView
    private lateinit var tvJiangtui: TextView
    private var currentBean: CollectBean = CollectBean()

    override fun getLayoutId(): Int {
        return R.layout.activity_6602_seat_adjust
    }

    override fun initView() {
        super.initView()
        setTitle(getString(R.string.seat_adjust))
        tvCurrentPro = findViewById(R.id.tv_current_program)
        tvRemainTime = findViewById(R.id.tv_remain_time)
        tvReset = findViewById(R.id.tv_reset)
        tvReset.setOnClickListener(this)
        tvZero1 = findViewById(R.id.tv_zero_1)
        tvZero1.setOnClickListener(this)
        tvZero2 = findViewById(R.id.tv_zero_2)
        tvZero2.setOnClickListener(this)
        tvDaobei = findViewById(R.id.tv_daobei)
        tvDaobei.setOnClickListener(this)
        tvShenbei = findViewById(R.id.tv_shenbei)
        tvShenbei.setOnClickListener(this)
        tvTaitui = findViewById(R.id.tv_taitui)
        tvTaitui.setOnClickListener(this)
        tvJiangtui = findViewById(R.id.tv_jiangtui)
        tvJiangtui.setOnClickListener(this)
        iv_collect.setOnClickListener(this)
//        tv_shentui.setOnClickListener(this)
//        tv_suotui.setOnClickListener(this)
        currentBean.typeCode = ProjectSpUtils.getCurDeviceTypeCode()
        Device6602DeviceMainActivity.collectViewModel.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Loading -> {
                    showLoadingDialog(canceledOnTouchOutside = false)
                }
                is LoadState.Fail -> {
                    dismissLoadingDialog()
                    ToastUtils.showShort(it.msg)
                }
                is LoadState.Success -> {
                    iv_collect.isSelected = it.msg == "1"
                    dismissLoadingDialog()
                }
            }
        })
    }

    @SuppressLint("SetTextI18n")
    override fun onDeviceStateChange() {
        super.onDeviceStateChange()
        val deviceStatusBean = Massage6602Armchair.getInstance().deviceStatusBean
        if (deviceStatusBean != null) {
            val autoProgram = deviceStatusBean.functions.autoProgram
            val autoValue = Integer.parseInt(autoProgram.modelValue)
            if (autoValue > 0) {
                currentBean.type = 1
                currentBean.program = autoProgram.modelValue
                currentBean.programName = autoProgram.itemName
                iv_collect.visibility = View.VISIBLE
                val bean =
                    Device6602DeviceMainActivity.collectViewModel.getCollectBean(1, autoValue)
                iv_collect.isSelected = bean != null
                tvCurrentPro.text = getString(R.string.current_program) + autoProgram.itemName
            }
            val healthProgram = deviceStatusBean.functions.lifeAutoProgram
            val healthValue = Integer.parseInt(healthProgram.modelValue)
            if (healthValue > 0) {
                currentBean.type = 6
                currentBean.program = healthProgram.modelValue
                currentBean.programName = healthProgram.itemName
                iv_collect.visibility = View.VISIBLE
                val bean = Device6602DeviceMainActivity.collectViewModel.getCollectBean(
                    6,
                    healthValue
                )
                iv_collect.isSelected = bean != null
                tvCurrentPro.text = getString(R.string.current_program) + healthProgram.itemName
            }
            val advanceProgram = deviceStatusBean.functions.advanceAutoProgram
            val advanceValue = advanceProgram.modelValue
            val min = deviceStatusBean.functions.timeMin.modelValue
            var sec = deviceStatusBean.functions.timeSec.modelValue
            if (sec.length < 2) {
                sec = "0$sec"
            }
            tvRemainTime.text = getString(R.string.remain_time) + min + getString(R.string.minute)
            if (advanceValue != "0") {
                iv_collect.visibility = View.GONE
                currentBean = CollectBean()
                tvCurrentPro.text =
                    getString(R.string.current_program) + getString(R.string.advance_massage)
            }
            if (autoValue == 0 && healthValue == 0 && advanceValue == "0") {//无程序运行
                iv_collect.visibility = View.GONE
                tvCurrentPro.text = getString(R.string.current_program)
                tvRemainTime.text = getString(R.string.remain_time)
            }
            val zeroGravity = deviceStatusBean.functions.zeroGravity
            val zeroValue = Integer.parseInt(zeroGravity.modelValue)
            tvReset.isSelected = zeroValue == 0
            tvZero1.isSelected = zeroValue == 1
            tvZero2.isSelected = zeroValue == 2
            val backPos = deviceStatusBean.functions.backPosAdjust
            val backPosValue = Integer.parseInt(backPos.modelValue)
            tvDaobei.isSelected = backPosValue == 0
            tvShenbei.isSelected = backPosValue == 1
            val legPos = deviceStatusBean.functions.legPosAdjust
            val legPosValue = Integer.parseInt(legPos.modelValue)
            tvJiangtui.isSelected = legPosValue == 0
            tvTaitui.isSelected = legPosValue == 1
            val legPosX = deviceStatusBean.functions.legPosAdjustX
            val legPosValueX = Integer.parseInt(legPosX.modelValue)
//            tv_shentui.isSelected = legPosValueX == 1
//            tv_suotui.isSelected = legPosValueX == 0
        } else {
            iv_collect.visibility = View.GONE
//            finish()
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_reset -> {
                publish(Constants.ZERO_GRAVITY, "0")
                DataCollectUtils.seatEvent(3, ProjectSpUtils.getCurDeviceTypeCode())
            }
            R.id.tv_zero_1 -> {
                publish(Constants.ZERO_GRAVITY, "1")
                DataCollectUtils.seatEvent(1, ProjectSpUtils.getCurDeviceTypeCode())
            }
            R.id.tv_zero_2 -> {
                publish(Constants.ZERO_GRAVITY, "2")
                DataCollectUtils.seatEvent(2, ProjectSpUtils.getCurDeviceTypeCode())
            }
            R.id.tv_daobei -> publish(Constants.BACK_POS_ADJUST, "0")
            R.id.tv_shenbei -> publish(Constants.BACK_POS_ADJUST, "1")
            R.id.tv_taitui -> publish(Constants.LEG_POS_ADJUST, "1")
            R.id.tv_jiangtui -> publish(Constants.LEG_POS_ADJUST, "0")
//            R.id.tv_shentui -> publish(Constants.LEG_POS_ADJUST_X, "1")
//            R.id.tv_suotui -> publish(Constants.LEG_POS_ADJUST_X, "0")
            R.id.iv_collect -> {
                if (iv_collect.isSelected) {
                    Device6602DeviceMainActivity.collectViewModel.unCollect(currentBean)
                } else {
                    Device6602DeviceMainActivity.collectViewModel.collect(currentBean)
                }
            }
        }
    }
}